package com.xunlai.infra.permission.business.service.impl;

import com.xunlai.infra.permission.business.converter.ScenicAreaConverter;
import com.xunlai.infra.permission.business.dao.entity.Area;
import com.xunlai.infra.permission.business.dao.entity.ScenicArea;
import com.xunlai.infra.permission.business.dao.repository.AreaRepository;
import com.xunlai.infra.permission.business.dao.repository.ScenicAreaRepository;
import com.xunlai.infra.permission.business.service.ScenicAreaService;
import com.xunlai.infra.permission.business.vo.scenic.*;
import com.xunlai.infra.permission.common.vo.BasePageRespVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;
import java.util.stream.StreamSupport;

/**
 * @author liang
 * @date 8/23/23 5:17 PM
 */
@Service
public class ScenicAreaServiceImpl implements ScenicAreaService {

    private ScenicAreaRepository scenicAreaRepository;

    private AreaRepository areaRepository;

    @Override
    public Long createScenicArea(ScenicAreaCreateVo scenicAreaCreateVo) {
        ScenicArea scenicArea = ScenicAreaConverter.INSTANCE.convertCreateVo2Entity(scenicAreaCreateVo);
        if (scenicArea.getCustom() == null) {
            scenicArea.setCustom(Boolean.FALSE);
        }
        ScenicArea save = scenicAreaRepository.save(scenicArea);
        return save.getId();
    }

    @Override
    public BasePageRespVo<ScenicAreaListVo> listScenicArea(ScenicAreaQueryVo scenicAreaQueryVo, Integer pageNum, Integer pageSize) {
        pageNum = (pageNum == null || pageNum < 1) ? 1 : pageNum;
        int size = (pageSize == null || pageSize <= 0) ? 10 : pageSize;
        Page<ScenicArea> scenicAreaPage = scenicAreaRepository.findAll(Example.of(ScenicArea.builder().custom(false).build()), PageRequest.of(pageNum - 1, size));
        List<ScenicAreaListVo> scenicAreaListVos = scenicAreaPage.getContent().stream().map(ScenicAreaConverter.INSTANCE::convertEntity2ListVo).toList();
        return new BasePageRespVo<>(scenicAreaListVos, pageNum, pageSize, scenicAreaPage.getTotalElements(), scenicAreaPage.getTotalPages());
    }

    @Override
    public BasePageRespVo<ScenicAreaListVo> listScenicArea(Long createUser, ScenicAreaQueryVo scenicAreaQueryVo, Integer pageNum, Integer pageSize) {
        pageNum = (pageNum == null || pageNum < 1) ? 1 : pageNum;
        int size = (pageSize == null || pageSize <= 0) ? 10 : pageSize;
        ScenicArea scenicArea = ScenicArea.builder().custom(true).build();
        scenicArea.setCreateUser(createUser);
        Page<ScenicArea> scenicAreaPage = scenicAreaRepository.findAll(Example.of(scenicArea), PageRequest.of(pageNum - 1, size));
        List<ScenicAreaListVo> scenicAreaListVos = scenicAreaPage.getContent().stream().map(ScenicAreaConverter.INSTANCE::convertEntity2ListVo).toList();
        return new BasePageRespVo<>(scenicAreaListVos, pageNum, pageSize, scenicAreaPage.getTotalElements(), scenicAreaPage.getTotalPages());
    }

    @Override
    public void deleteScenicArea(Long id) {
        scenicAreaRepository.deleteById(id);
    }

    @Override
    public void updateScenicArea(Long id, ScenicAreaCreateVo scenicAreaCreateVo) {
        Optional<ScenicArea> scenicAreaOptional = scenicAreaRepository.findById(id);
        if (scenicAreaOptional.isPresent()) {
            ScenicArea scenicArea = scenicAreaOptional.get();
            BeanUtils.copyProperties(scenicAreaCreateVo, scenicArea);
            scenicAreaRepository.save(scenicArea);
        }
    }

    @Override
    public List<ScenicAreaListVo> listAllScenicArea() {
        Iterable<ScenicArea> all = scenicAreaRepository.findAll(Example.of(ScenicArea.builder().custom(false).build()));
        return StreamSupport.stream(all.spliterator(), false).map(ScenicAreaConverter.INSTANCE::convertEntity2ListVo).toList();
    }

    @Override
    public List<ScenicAreaListVo> listAllScenicArea(Long createUser) {
        ScenicArea scenicArea = ScenicArea.builder().custom(true).build();
        scenicArea.setCreateUser(createUser);
        Iterable<ScenicArea> all = scenicAreaRepository.findAll(Example.of(scenicArea));
        return StreamSupport.stream(all.spliterator(), false).map(ScenicAreaConverter.INSTANCE::convertEntity2ListVo).toList();
    }

    @Override
    public List<ProvinceListVo> listAllProvince() {
        Area area = new Area();
        area.setPid(0L);
        Iterable<Area> all = areaRepository.findAll(Example.of(area));
        return StreamSupport.stream(all.spliterator(), false).map(ScenicAreaConverter.INSTANCE::convertAreaEntity2ProvinceListVo).toList();
    }

    @Override
    public List<AreaListVo> listAllArea() {
        Iterable<Area> all = areaRepository.findAll();
        return StreamSupport.stream(all.spliterator(), false).map(ScenicAreaConverter.INSTANCE::convertAreaEntity2ListVo).toList();
    }

    @Autowired
    public void setScenicAreaRepository(ScenicAreaRepository scenicAreaRepository) {
        this.scenicAreaRepository = scenicAreaRepository;
    }

    @Autowired
    public void setAreaRepository(AreaRepository areaRepository) {
        this.areaRepository = areaRepository;
    }
}
